{extends "../@layout.xml"}

{block title}
    {tr("search_for_$type")}
    {if $_GET['query']}
        - {$_GET['query']}
    {/if}
{/block}

{block header}
    {=OPENVK_ROOT_CONF["openvk"]["appearance"]["name"]} »
    {tr("search_for_$type")}
{/block}

{* BEGIN ELEMENTS DESCRIPTION *}

{block link|strip|stripHtml}
    {if $type != "apps"}
        {$x->getURL()}
    {else}
        /app{$x->getId()}
    {/if}
{/block}

{block preview}
    <img src="{$x->getAvatarUrl('miniscule')}" width="75" alt="{_photo}" loading=lazy />
{/block}

{block name}
    {if $type != "apps"}
        <text style="overflow: hidden;">&nbsp;{$x->getCanonicalName()}        
        {if $_GET['sort'] == "rating"}({$x->getRating()}%)
        {elseif $_GET['sort'] == "id"}(id{$x->getId()}){/if}</text>
        <img n:if="$x->isVerified()" 
            class="name-checkmark"
            src="/assets/packages/static/openvk/img/checkmark.png"
        />
    {else}
        {$x->getName()}
    {/if}
{/block}

{block description}
    <table class="ugc-table">
        <tbody>
            {if $type === "users"}
                {if !is_null($_GET['status']) && $_GET['status'] != ""}
                    <tr>
                        <td><span class="nobold">{_status}:</span></td>
                        <td>{$x->getStatus()}</td>
                    </tr>
                {/if}
                {if !is_null($_GET['city']) && $_GET['city'] != "" && $x->getPrivacySetting("page.info.read") > 1}
                    <tr>
                        <td><span class="nobold">{_city}:</span></td>
                        <td>{$x->getCity()}</td>
                    </tr>
                {/if}
                {if !is_null($_GET['city']) && $_GET['hometown'] != "" && $x->getPrivacySetting("page.info.read") > 1}
                    <tr>
                        <td><span class="nobold">{_hometown}:</span></td>
                        <td>{$x->getHometown()}</td>
                    </tr>
                {/if}
                {if !is_null($_GET['politViews']) && $_GET['politViews'] != 0 && $x->getPrivacySetting("page.info.read") > 1}
                    <tr>
                        <td><span class="nobold">{_politViews}:</span></td>
                        <td>{tr("politViews_".$x->getPoliticalViews())}</td>
                    </tr>
                {/if}
                {if !is_null($_GET['email']) && $_GET['email'] != "" && $x->getPrivacySetting("page.info.read") > 1}
                    <tr>
                        <td><span class="nobold">{_email}:</span></td>
                        <td>{$x->getContactEmail()}</td>
                    </tr>
                {/if}
                {if !is_null($_GET['telegram']) && $_GET['telegram'] != "" && $x->getPrivacySetting("page.info.read") > 1}
                    <tr>
                        <td><span class="nobold">{_telegram}:</span></td>
                        <td><a href="tg://resolve?domain={$x->getTelegram()}">@{$x->getTelegram()}</a></td>
                    </tr>
                {/if}
                {if !is_null($_GET['site']) && $_GET['site'] != "" && $x->getPrivacySetting("page.info.read") > 1}
                    <tr>
                        <td><span class="nobold">{_personal_website}:</span></td>
                        <td><a href="{$x->getWebsite()}">{$x->getWebsite()}</a></td>
                    </tr>
                {/if}
                {if !is_null($_GET['address']) && $_GET['address'] != "" && $x->getPrivacySetting("page.info.read") > 1}
                    <tr>
                        <td><span class="nobold">{_address}:</span></td>
                        <td>{$x->getPhysicalAddress()}</td>
                    </tr>
                {/if}    
                {if $x->getPrivacySetting("page.info.read") > 1}        
                <tr>
                    <td><span class="nobold">{_pronouns}: </span></td>
                    <td>{$x->isFemale() ? tr("female") : ($x->isNeutral() ? tr("neutral") : tr("male"))}</td>
                </tr>
                <tr>
                    <td><span class="nobold">{_relationship}:</span></td>
                    <td>{$x->getLocalizedMaritalStatus()}</td>
                </tr>
                <tr>
                    <td><span class="nobold">{_registration_date}: </span></td>
                    <td>{$x->getRegistrationTime()}</td>
                </tr>
                {/if}
            {/if}
            <tr>
            {if !empty($x->getDescription())}
                {if $type != "apps"}
                <td>
                    <span class="nobold">{_description}:</span>
                </td>
                {/if}
                <td {if $type == "apps"}style="width:400px"{/if}>
                    {$x->getDescription() ?? '(' . tr("none") . ')'}
                </td>
            </tr>
            {/if}
            {if $type == "groups"}
                <td>
                    <span class="nobold">{_size}:</span>
                </td>
                <td>
                    {tr("participants", $x->getFollowersCount())}
                </td>
            {/if}
        </tbody>
    </table>
{/block}

{block content}
    <style>
    .comment, .post-divider 
    {
        border:none;
    }
    </style>
    <div style="margin-top:-7px">
    {var $data = is_array($iterator) ? $iterator : iterator_to_array($iterator)}

    {include "../components/paginator.xml", conf => (object) [
        "page"     => $page,
        "count"    => $count,
        "amount"   => sizeof($data),
        "perPage"  => $perPage ?? OPENVK_DEFAULT_PER_PAGE,
        "atBottom" => false,
    ]}
    </div>
    <p style="margin-left: 15px; margin-top: 0;">
        <b>{tr("results", $count)}</b>
    </p>
    <div>
    {include searchOptions}

    <div class="container_gray borderup" style="float:left;width:73.3%;">
        {if sizeof($data) > 0}
            {if $type == "users" || $type == "groups" || $type == "apps"}
                <div class="content" n:foreach="$data as $dat">
                    <table>
                    <tbody>
                        <tr>
                            <td valign="top">
                                <a href="{include link, x => $dat}">
                                {include preview, x => $dat}
                                </a>
                            </td>
                            <td valign="top" style="width: 100%">
                                {ifset infotable}
                                    {include infotable, x => $dat}
                                {else}
                                    <a href="{include link, x => $dat}">
                                    <b>
                                        {include name, x => $dat}
                                    </b>
                                    </a>
                                    <br/>
                                    {include description, x => $dat}
                                    {/ifset}
                            </td>
                            <td n:ifset="actions" valign="top" class="action_links" style="width: 150px;">
                                {include actions, x => $dat}
                            </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            {elseif $type == "posts"}
                <div n:foreach="$data as $dat" class="content">
                    {if !$dat || $dat->getTargetWall() < 0 && $dat->getWallOwner()->isHideFromGlobalFeedEnabled() || !$dat->canBeViewedBy($thisUser)}
                        {_closed_group_post}.
                    {else}
                        {include "../components/post.xml", post => $dat, commentSection => true, onWallOf => true}
                    {/if}
                </div>
            {elseif $type == "comments"}
                <div n:foreach="$data as $dat" class="content">
                    {if !$dat->getTarget() || $dat->getTarget()->isDeleted()}
                        {_deleted_target_comment}.
                    {else}
                        {include "../components/comment.xml", comment => $dat, linkW => true}
                    {/if}
                </div>
            {elseif $type == "videos"}
                {foreach $data as $dat}
                    <div class="content">
                        {include "../components/video.xml", video => $dat}
                    </div>
                {/foreach}
            {elseif $type == "audios"}
                {foreach $data as $dat}
                    {include "../Audio/player.xml", audio => $dat}
                {/foreach}
            {/if}
        {else}
            {ifset customErrorMessage}
                {include customErrorMessage}
            {else}
                {include "../components/nothing.xml"}
            {/ifset}
        {/if}
    </div>
    <script>
        window.addEventListener("load", (event) => {
        document.getElementsByClassName("container_gray")[0].style.minHeight = document.getElementsByClassName("searchOptions")[0].clientHeight+"px";
        document.getElementsByClassName("searchOptions")[0].style.minHeight = document.getElementsByClassName("container_gray")[0].clientHeight-3+"px";
        })
    </script>
{/block}

{block searchOptions}
    <div class="searchOptions">
        <ul class="searchList">
            <a {if $type === "users"}   id="used"{/if} href="/search?type=users{if $_GET['query']}&query={urlencode($_GET['query'])}{/if}&sort=id">   {_s_people}   {if $type === "users"}   ({$count}){/if}</a>
            <a {if $type === "groups"}  id="used"{/if} href="/search?type=groups{if $_GET['query']}&query={urlencode($_GET['query'])}{/if}&sort=id">  {_s_groups}   {if $type === "groups"}  ({$count}){/if}</a>
            <a {if $type === "comments"}id="used"{/if} href="/search?type=comments{if $_GET['query']}&query={urlencode($_GET['query'])}{/if}&sort=id">{_s_comments} {if $type === "comments"}({$count}){/if}</a>
            <a {if $type === "posts"}   id="used"{/if} href="/search?type=posts{if $_GET['query']}&query={urlencode($_GET['query'])}{/if}&sort=id">   {_s_posts}    {if $type === "posts"}   ({$count}){/if}</a>
            <a {if $type === "videos"}  id="used"{/if} href="/search?type=videos{if $_GET['query']}&query={urlencode($_GET['query'])}{/if}&sort=id">  {_s_videos}   {if $type === "videos"}  ({$count}){/if}</a>
            <a {if $type === "apps"}    id="used"{/if} href="/search?type=apps{if $_GET['query']}&query={urlencode($_GET['query'])}{/if}&sort=id">    {_s_apps}     {if $type === "apps"}    ({$count}){/if}</a>
            <a {if $type === "audios"}  id="used"{/if} href="/search?type=audios{if $_GET['query']}&query={urlencode($_GET['query'])}{/if}&sort=id">  {_s_audios}   {if $type === "audios"}  ({$count}){/if}</a>
        </ul>

        <div class="searchOption">
            <div class="searchOptionName" id="n_sort" onclick="hideParams('sort')"><img src="/assets/packages/static/openvk/img/hide.png" class="searchHide">{_s_order_by}</div>
            <div class="searchOptionBlock" id="s_sort">
                <select name="sort" form="searcher" id="sortyor">
                    <option value="id" {if $_GET["sort"] == "name"}selected{/if}>{_s_order_by_id}</option>
                    {if $type == "users"}
                        <option value="name" {if $_GET["sort"] == "name"}selected{/if}>{_s_order_by_name}</option>
                        {if OPENVK_ROOT_CONF["openvk"]["preferences"]["commerce"]}
                            <option value="rating" {if $_GET["sort"] == "rating"}selected{/if}>{_s_order_by_rating}</option>
                        {/if}
                    {/if}

                    {if $type == "audios"}
                        <option value="length" n:attr="selected => $_GET['sort'] == 'length'">{_s_order_by_length}</option>
                        <option value="listens" n:attr="selected => $_GET['sort'] == 'listens'">{_s_order_by_listens}</option>
                    {/if}
                </select>
                <div id="invertor">
                <input type="checkbox" name="invert" value="1" form="searcher" {if !is_null($_GET['invert']) && $_GET['invert'] == "1"}checked{/if}>{_s_order_invert}
                </div>
            </div>
        </div>

    {if $type !== "groups" && $type !== "apps"} {* В OpenVK не сохраняется дата создания групп и приложений *}
        <div class="searchOption">
            <div class="searchOptionName" id="n_dates" onclick="hideParams('dates')"><img src="/assets/packages/static/openvk/img/hide.png" class="searchHide">{_s_by_date}</div>
            <div class="searchOptionBlock" id="s_dates">
                {if $type != "users"}
                    <p id="bydate">{_s_date_before}:<br>
                {else}
                    <p id="bydate">{_s_registered_before}:<br>
                {/if}
                <input type="date" name="datebefore" 
                    form="searcher"
                    id="bydate"
                    style="width:100%"
                    value="{if !is_null($_GET['datebefore'])}{$_GET['datebefore']}{/if}"
                    min="2019-11-19"
                    max={date('Y-m-d')}></p>
                {if $type != "users"}
                    <p id="bydate">{_s_date_after}:<br>
                {else}
                    <p id="bydate">{_s_registered_after}:<br>
                {/if}
                <input type="date" name="dateafter" 
                    form="searcher"
                    style="width:100%"
                    id="bydate"
                    value="{if !is_null($_GET['dateafter'])}{$_GET['dateafter']}{/if}"
                    min="2019-11-18"
                    max={date('Y-m-d')}></p>
            </div>
        </div>
    {/if}

    {if $type === "users"}
        <div class="searchOption">
            <div class="searchOptionName" id="n_main" onclick="hideParams('main')"><img src="/assets/packages/static/openvk/img/hide.png" class="searchHide">{_main}</div>
            <div class="searchOptionBlock" id="s_main">
                <input type="text" value="{if !is_null($_GET['status'])}{$_GET['status']}{/if}" form="searcher" placeholder="{_status}" name="status">
                <input type="text" value="{if !is_null($_GET['city'])}{$_GET['city']}{/if}" form="searcher" placeholder="{_city}" name="city">
                <input type="text" value="{if !is_null($_GET['hometown'])}{$_GET['hometown']}{/if}" form="searcher" placeholder="{_hometown}" name="hometown">
                <input name="is_online" type="checkbox" {if !is_null($_GET['is_online']) && $_GET['is_online'] == "1"}checked{/if} form="searcher" value="1">{_s_now_on_site}
            </div>
        </div>

        <!-- <div class="searchOption">
            <div class="searchOptionName" id="n_gender" onclick="hideParams('gender')"><img src="/assets/packages/static/openvk/img/hide.png" class="searchHide">{_gender}</div>
            <div class="searchOptionBlock" id="s_gender">
                <p><input type="radio" form="searcher" id="gend" {if $_GET['gender'] == 0}checked{/if} name="gender" value="0">{_male}</p>
                <p><input type="radio" form="searcher" id="gend1"{if $_GET['gender'] == 1}checked{/if} name="gender" value="1">{_female}</p>
                <p><input type="radio" form="searcher" id="gend2"{if $_GET['gender'] == 2 || is_null($_GET['gender'])}checked{/if} name="gender" value="2">{_s_any}</p>
            </div>
        </div> -->

        <div class="searchOption">
            <div class="searchOptionName" id="n_relationship" onclick="hideParams('relationship')"><img src="/assets/packages/static/openvk/img/hide.png" class="searchHide">{ovk_proc_strtr(tr("relationship"), 14)}</div>
            <div class="searchOptionBlock" id="s_relationship">
                <select name="maritalstatus" form="searcher">
                    <option n:foreach="range(0, 8) as $i" value="{$i}" {if $_GET['maritalstatus'] == $i}selected{/if} form="searcher">
                        {tr("relationship_".$i)}
                    </option>
                </select>
            </div>
        </div>

        <div class="searchOption">
            <div class="searchOptionName" id="n_politViews" onclick="hideParams('politViews')"><img src="/assets/packages/static/openvk/img/hide.png" class="searchHide">{_politViews}</div>
            <div class="searchOptionBlock" id="s_politViews">
                <select name="politViews" form="searcher">
                    <option n:foreach="range(0, 9) as $i" value="{$i}" {if $_GET['politViews'] == $i}selected{/if} form="searcher">
                        {tr("politViews_".$i)}
                    </option>
                </select>
            </div>
        </div>

        <div class="searchOption">
            <div class="searchOptionName" id="n_contacts" onclick="hideParams('contacts')"><img src="/assets/packages/static/openvk/img/hide.png" class="searchHide">{_contacts}</div>
            <div class="searchOptionBlock" id="s_contacts">
                <input type="text" name="email"    value="{if !is_null($_GET['email'])}{$_GET['email']}{/if}" form="searcher" placeholder="{_email}">
                <input type="text" name="telegram" value="{if !is_null($_GET['telegram'])}{$_GET['telegram']}{/if}" form="searcher" placeholder="{_telegram}"><br id="contacts">
                <input type="text" name="site"     value="{if !is_null($_GET['site'])}{$_GET['site']}{/if}" form="searcher" placeholder="{_personal_website}"><br id="contacts">
                <input type="text" name="address"  value="{if !is_null($_GET['address'])}{$_GET['address']}{/if}" form="searcher" placeholder="{_address}">
            </div>
        </div>

        <div class="searchOption">
            <div class="searchOptionName" id="n_interests" onclick="hideParams('interests')"><img src="/assets/packages/static/openvk/img/hide.png" class="searchHide">{_interests}</div>
            <div class="searchOptionBlock" id="s_interests">
                <input type="text" value="{if !is_null($_GET['interests'])}{$_GET['interests']}{/if}" form="searcher" placeholder="{_interests}" name="interests">
                <input type="text" value="{if !is_null($_GET['fav_mus'])}{$_GET['fav_mus']}{/if}"     form="searcher" placeholder="{_favorite_music}" name="fav_mus">
                <input type="text" value="{if !is_null($_GET['fav_films'])}{$_GET['fav_films']}{/if}" form="searcher" placeholder="{_favorite_films}" name="fav_films">
                <input type="text" value="{if !is_null($_GET['fav_shows'])}{$_GET['fav_shows']}{/if}" form="searcher" placeholder="{_favorite_shows}" name="fav_shows">
                <input type="text" value="{if !is_null($_GET['fav_books'])}{$_GET['fav_books']}{/if}" form="searcher" placeholder="{_favorite_books}" name="fav_books">
                <input type="text" value="{if !is_null($_GET['fav_quote'])}{$_GET['fav_quote']}{/if}" form="searcher" placeholder="{_favorite_quotes}"  name="fav_quote">
            </div>
        </div>
    {/if}

    {if $type == "audios"}
        <div class="searchOption">
        <div class="searchOptionName" id="n_main_audio" onclick="hideParams('main_audio')"><img src="/assets/packages/static/openvk/img/hide.png" class="searchHide">{_s_main}</div>
            <div class="searchOptionBlock" id="s_main_audio">
                <label><input type="checkbox" name="only_performers" n:attr="checked => !empty($_GET['only_performers'])" form="searcher">{_s_only_performers}</label><br>
                <label><input type="checkbox" name="with_lyrics" n:attr="checked => !empty($_GET['with_lyrics'])" form="searcher">{_s_with_lyrics}</label>
            </div>
        </div>
    {/if}
    
    <input class="button" type="button" id="dnt" value="{_reset}" onclick="resetSearch()">
    </div>

{/block}
